/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.web.backing;

import com.factory.web.dto.UserDTO;
import com.factory.web.json.Stats;
import com.factory.web.service.FactoryServiceInterface;
import com.factory.web.service.problems.ProblemEnum;
import com.factory.web.support.FacesUtil;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author milos
 */
@Component("adminBackingBean")
@Scope("session")
public class AdminBackingBean {
    
    private List<UserDTO> allUsers;
    private Stats stats;
    @Autowired
    private FactoryServiceInterface factoryService;
    
    public List<UserDTO> getAllUsers() {
        allUsers = factoryService.getAllUsers();
        return allUsers;
    }
    
    public void setAllUsers(List<UserDTO> allUsers) {
        this.allUsers = allUsers;
    }
    
    public void blockingUser(UserDTO userDTO) { //Blocking
        userDTO.setBlocked(true);
        factoryService.updateUser(userDTO);
        FacesUtil.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", "User blocked."));
    }
    
    public void unblockingUser(UserDTO userDTO) { //Unblocking
        userDTO.setBlocked(false);
        factoryService.updateUser(userDTO);
        FacesUtil.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", "User unblocked."));
    }
    
    public CartesianChartModel getResolvedCategoryModel() {
        if (stats == null) {
            stats = factoryService.getStats();
        }
        CartesianChartModel categoryModel = new CartesianChartModel();
        ChartSeries chart1 = new ChartSeries();
        chart1.setLabel("Resolved count");
        
        Map<String, Integer> problemCountMapping = this.stats.getResolvedProblemCountMap();
        for (String key : problemCountMapping.keySet()) {
            chart1.set(ProblemEnum.valueOf(key).getCode(), problemCountMapping.get(key));
        }
        categoryModel.addSeries(chart1);
        return categoryModel;
    }
    
    public CartesianChartModel getScheduledCategoryModel() {
        if (stats == null) {
            stats = factoryService.getStats();
        }
        CartesianChartModel categoryModel = new CartesianChartModel();
        ChartSeries chart1 = new ChartSeries();
        chart1.setLabel("Scheduled count");
        Map<String, Integer> problemCountMapping = this.stats.getScheduledProblemCountMap();
        for (String key : problemCountMapping.keySet()) {
            chart1.set(ProblemEnum.valueOf(key).getCode(), problemCountMapping.get(key));
        }
        categoryModel.addSeries(chart1);
        return categoryModel;
    }
    
    public PieChartModel getPieModel() {
        if (stats == null) {
            stats = factoryService.getStats();
        }
        PieChartModel pieModel = new PieChartModel();
        
        int countResolved = 0;
        for (int i : stats.getResolvedProblemCountMap().values()) {
            countResolved += i;
        }
        
        int countScheduled = 0;
        for (int i : stats.getScheduledProblemCountMap().values()) {
            countScheduled += i;
        }
        
        pieModel.set("Resolved", countResolved);
        pieModel.set("Scheduled", countScheduled);
        
        return pieModel;
    }
}
